package com.zwl.smartplatform.events.listeners;

import com.zwl.smartplatform.events.UserLoginEvent;
import com.zwl.smartplatform.mapper.UserMapper;
import com.zwl.smartplatform.mapper.UserOnlineMapper;
import com.zwl.smartplatform.pojo.User;
import com.zwl.smartplatform.pojo.UserOnline;
import com.zwl.smartplatform.service.UserService;
import com.zwl.smartplatform.utils.RequestUtil;
import com.zwl.smartplatform.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Component
public class UserLoginListener {

    private UserService userService;

    private UserOnlineMapper userOnlineMapper;

    @Autowired
    public void setUserOnlineMapper(UserOnlineMapper userOnlineMapper) {
        this.userOnlineMapper = userOnlineMapper;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    //更新用户到基本登录信息
    @EventListener
    //  @Async
    public void upDateUserInfo(UserLoginEvent userLoginEvent){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        User user = SecurityUtils.getUser();
        //通过异步执行当前操作 内部有自己到线程池
        CompletableFuture.runAsync(()->{//runAsync不需要结果到异步
            try {
            //    Map<String, String> osAndBrowserInfo = RequestUtil.getOsAndBrowserInfo(request);//存放了是请求的系统和软件信息
                String ip = com.zwl.smartplatform.utils.RequestUtil.getRemoteHost(request);//获取到用户到ip
                //  user.setLastLogin(user.getCurrentLogin());
                user.setCurrentLogin(new Date());
                //   user.setLastLoginIp(user.getCurrentLoginIp());
                user.setCurrentLoginIp(ip);
                userService.updateUserLogin(user);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
//      new Thread(){
//          @Override
//          public void run() {
//              try {
//
//              } catch (Exception e){
//                  e.printStackTrace();
//              }
//          }
//      }.start();
    }
    //更新用户到在线状态
    @EventListener
    //@Async
    public void upDateUserOnline(UserLoginEvent userLoginEvent){
        HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        User user = SecurityUtils.getUser();
        CompletableFuture.runAsync(()->{
            try {
                Map<String, String> osAndBrowserInfo = com.zwl.smartplatform.utils.RequestUtil.getOsAndBrowserInfo(request);//存放了是请求的系统和软件信息
                String ip = com.zwl.smartplatform.utils.RequestUtil.getRemoteHost(request);//获取到用户到ip
                UserOnline userOnline=new UserOnline();
                userOnline.setSessionId(request.getSession().getId());
                userOnline.setLoginName(user.getUsername());
                userOnline.setStartTimestamp(new Date());
                userOnline.setStatus("1");
                userOnline.setIpaddr(ip);
                userOnline.setLoginLocation(com.zwl.smartplatform.utils.RequestUtil.getLocationByIp(ip));//请求第三方服务器 需要时间 应该放到单独到线程中执行
                userOnline.setOs(osAndBrowserInfo.get("os"));
                userOnline.setBrowser(osAndBrowserInfo.get("browser"));
                userOnline.setExpireTime(30L);
                //保存到数据库 发现有问题 当前用户到数据如果已经存在了就不可以更新
                UserOnline currentLogin = userOnlineMapper.selectByLoginName(user.getUsername());
                if (currentLogin!=null){
                    //存在 删除
                    userOnlineMapper.deleteByLoginName(user.getUsername());
                }
                userOnlineMapper.addOnlineData(userOnline);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }
    //更新登录日志
    @EventListener
   // @Async
    public void onLoginLog(UserLoginEvent userLoginEvent){

    }
}
